package com.sys.controller;


import org.hibernate.service.spi.ServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/** 控制器类的基类 */
public class BaseController {
    /** 操作成功的状态码 */
    public static final int OK = 200;
    public static final int EOK = 0;

    /**
     * 从HttpSession对象中获取uid
     * @param session HttpSession对象
     * @return 当前登录的用户的id
     */
    protected final Integer getUidFromSession(HttpSession session) {
            Object uidObj = session.getAttribute("uid");
            if (uidObj == null) {
                // 可以记录日志或抛出自定义异常
                System.out.println("Session attribute 'uid' is not set.");
                throw new IllegalStateException("Session attribute 'uid' is not set.");
            }
            return Integer.valueOf(uidObj.toString());

    }

    /**
     * 从HttpSession对象中获取用户名
     * @param session HttpSession对象
     * @return 当前登录的用户名
     */
    protected final String getUsernameFromSession(HttpSession session) {
        return session.getAttribute("username").toString();
    }
}
